package com.jpyl.jpMedicalTreatment.view.fragment;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.sdkumeng.share.BHaiGouShareContent;
import com.example.sdkumeng.share.ShareHelper;
import com.forxindu.kit.box.ViewKit;
import com.jpyl.jpMedicalTreatment.MedicalApplication;
import com.jpyl.jpMedicalTreatment.R;
import com.jpyl.jpMedicalTreatment.common.Constant;
import com.jpyl.jpMedicalTreatment.model.WeiXinPayModel;
import com.jpyl.jpMedicalTreatment.view.activity.BaseActivity;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import butterknife.ButterKnife;

public abstract class BaseFragment extends Fragment {

    protected Context context;
    protected MedicalApplication medicalApplication;
    protected View mRootView;
    protected ShareHelper shareHelper;
    protected PopupWindow sharePopupWindow;

    public BaseFragment() {

    }

    public BaseFragment(Context context) {
        // TODO Auto-generated constructor stub
        this.context = context;
        medicalApplication = (MedicalApplication) ((Activity) context).getApplication();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        mRootView = inflater.inflate(getLayoutId(), container, false);

//        Observable.interval(1, TimeUnit.SECONDS)
//                .compose(this.bindToLifecycle())
//                .subscribe();

        return mRootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);
        shareHelper = new ShareHelper(getActivity());
        afterCreate(savedInstanceState);
    }

    protected abstract int getLayoutId();

    protected abstract void afterCreate(Bundle savedInstanceState);


    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }


    //用户分享Window
    public void showShareWindow(BHaiGouShareContent shareContent) {

        View shareLayout = View.inflate(getActivity(), R.layout.share_dialog, null);
        LinearLayout shareWeixin = shareLayout.findViewById(R.id.share_weixin_btn);
        LinearLayout shareWeixinFriends = shareLayout.findViewById(R.id.share_weixin_friends_btn);
        LinearLayout shareSina = shareLayout.findViewById(R.id.share_sina_btn);
        LinearLayout outSideLayout =  shareLayout.findViewById(R.id.outside_layout);
        LinearLayout qqZone = shareLayout.findViewById(R.id.share_qq_zone_btn);
        TextView cancelButton =  shareLayout.findViewById(R.id.cancel_btn);
        shareHelper.setShareContent(shareContent);
        outSideLayout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                sharePopupWindow.dismiss();
            }
        });
        shareWeixin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                shareHelper.weixin();
                sharePopupWindow.dismiss();
            }
        });
        shareWeixinFriends.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                shareHelper.weixinFriends();
                sharePopupWindow.dismiss();
            }
        });
        shareSina.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                shareHelper.sinaShare();
                sharePopupWindow.dismiss();
            }
        });
        qqZone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareHelper.qqZoneShare();
                sharePopupWindow.dismiss();
            }
        });
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sharePopupWindow.dismiss();
            }
        });
        sharePopupWindow = new PopupWindow(shareLayout, -1, -2, true);
        sharePopupWindow.setBackgroundDrawable(new BitmapDrawable());
        sharePopupWindow.setAnimationStyle(R.style.popupAnimation);
        sharePopupWindow.showAtLocation(((ViewGroup) getActivity().findViewById(android.R.id.content)).getChildAt(0), Gravity.CENTER, 0, 0);
        shareHelper.setUmShareListener(new UMShareListener() {
            @Override
            public void onResult(SHARE_MEDIA share_media) {
                ViewKit.showToast(getActivity(), "分享成功");
            }

            @Override
            public void onError(SHARE_MEDIA share_media, Throwable throwable) {

            }

            @Override
            public void onCancel(SHARE_MEDIA share_media) {

            }
        });
    }

}
